<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>scribblearea.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-widgets-scribble-example.html">Scribble Example</a></td><td >scribblearea.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">scribblearea.cpp 示例文件</h1>
<span class="subtitle">widgets/scribble/scribblearea.cpp</span>
<!-- $$$widgets/scribble/scribblearea.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>
  <span class="preprocessor">#if defined(QT_PRINTSUPPORT_LIB)</span>
  <span class="preprocessor">#include &lt;QtPrintSupport/qtprintsupportglobal.h&gt;</span>
  <span class="preprocessor">#if QT_CONFIG(printdialog)</span>
  <span class="preprocessor">#include &lt;QPrinter&gt;</span>
  <span class="preprocessor">#include &lt;QPrintDialog&gt;</span>
  <span class="preprocessor">#endif</span>
  <span class="preprocessor">#endif</span>

  <span class="preprocessor">#include &quot;scribblearea.h&quot;</span>

  ScribbleArea<span class="operator">::</span>ScribbleArea(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
  {
      setAttribute(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>WA_StaticContents);
      modified <span class="operator">=</span> <span class="keyword">false</span>;
      scribbling <span class="operator">=</span> <span class="keyword">false</span>;
      myPenWidth <span class="operator">=</span> <span class="number">1</span>;
      myPenColor <span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>blue;
  }

  bool ScribbleArea<span class="operator">::</span>openImage(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>fileName)
  {
      <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> loadedImage;
      <span class="keyword">if</span> (<span class="operator">!</span>loadedImage<span class="operator">.</span>load(fileName))
          <span class="keyword">return</span> <span class="keyword">false</span>;

      <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> newSize <span class="operator">=</span> loadedImage<span class="operator">.</span>size()<span class="operator">.</span>expandedTo(size());
      resizeImage(<span class="operator">&amp;</span>loadedImage<span class="operator">,</span> newSize);
      image <span class="operator">=</span> loadedImage;
      modified <span class="operator">=</span> <span class="keyword">false</span>;
      update();
      <span class="keyword">return</span> <span class="keyword">true</span>;
  }

  bool ScribbleArea<span class="operator">::</span>saveImage(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>fileName<span class="operator">,</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>fileFormat)
  {
      <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> visibleImage <span class="operator">=</span> image;
      resizeImage(<span class="operator">&amp;</span>visibleImage<span class="operator">,</span> size());

      <span class="keyword">if</span> (visibleImage<span class="operator">.</span>save(fileName<span class="operator">,</span> fileFormat)) {
          modified <span class="operator">=</span> <span class="keyword">false</span>;
          <span class="keyword">return</span> <span class="keyword">true</span>;
      } <span class="keyword">else</span> {
          <span class="keyword">return</span> <span class="keyword">false</span>;
      }
  }

  <span class="type">void</span> ScribbleArea<span class="operator">::</span>setPenColor(<span class="keyword">const</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> <span class="operator">&amp;</span>newColor)
  {
      myPenColor <span class="operator">=</span> newColor;
  }

  <span class="type">void</span> ScribbleArea<span class="operator">::</span>setPenWidth(<span class="type">int</span> newWidth)
  {
      myPenWidth <span class="operator">=</span> newWidth;
  }

  <span class="type">void</span> ScribbleArea<span class="operator">::</span>clearImage()
  {
      image<span class="operator">.</span>fill(<a href="../qtgui/qcolor.html#qRgb">qRgb</a>(<span class="number">255</span><span class="operator">,</span> <span class="number">255</span><span class="operator">,</span> <span class="number">255</span>));
      modified <span class="operator">=</span> <span class="keyword">true</span>;
      update();
  }

  <span class="type">void</span> ScribbleArea<span class="operator">::</span>mousePressEvent(<span class="type"><a href="../qtgui/qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>button() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftButton) {
          lastPoint <span class="operator">=</span> event<span class="operator">-</span><span class="operator">&gt;</span>pos();
          scribbling <span class="operator">=</span> <span class="keyword">true</span>;
      }
  }

  <span class="type">void</span> ScribbleArea<span class="operator">::</span>mouseMoveEvent(<span class="type"><a href="../qtgui/qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> ((event<span class="operator">-</span><span class="operator">&gt;</span>buttons() <span class="operator">&amp;</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftButton) <span class="operator">&amp;</span><span class="operator">&amp;</span> scribbling)
          drawLineTo(event<span class="operator">-</span><span class="operator">&gt;</span>pos());
  }

  <span class="type">void</span> ScribbleArea<span class="operator">::</span>mouseReleaseEvent(<span class="type"><a href="../qtgui/qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>button() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftButton <span class="operator">&amp;</span><span class="operator">&amp;</span> scribbling) {
          drawLineTo(event<span class="operator">-</span><span class="operator">&gt;</span>pos());
          scribbling <span class="operator">=</span> <span class="keyword">false</span>;
      }
  }

  <span class="type">void</span> ScribbleArea<span class="operator">::</span>paintEvent(<span class="type"><a href="../qtgui/qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="keyword">this</span>);
      <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> dirtyRect <span class="operator">=</span> event<span class="operator">-</span><span class="operator">&gt;</span>rect();
      painter<span class="operator">.</span>drawImage(dirtyRect<span class="operator">,</span> image<span class="operator">,</span> dirtyRect);
  }

  <span class="type">void</span> ScribbleArea<span class="operator">::</span>resizeEvent(<span class="type"><a href="../qtgui/qresizeevent.html">QResizeEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (width() <span class="operator">&gt;</span> image<span class="operator">.</span>width() <span class="operator">|</span><span class="operator">|</span> height() <span class="operator">&gt;</span> image<span class="operator">.</span>height()) {
          <span class="type">int</span> newWidth <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMax">qMax</a>(width() <span class="operator">+</span> <span class="number">128</span><span class="operator">,</span> image<span class="operator">.</span>width());
          <span class="type">int</span> newHeight <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMax">qMax</a>(height() <span class="operator">+</span> <span class="number">128</span><span class="operator">,</span> image<span class="operator">.</span>height());
          resizeImage(<span class="operator">&amp;</span>image<span class="operator">,</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(newWidth<span class="operator">,</span> newHeight));
          update();
      }
      <span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>resizeEvent(event);
  }

  <span class="type">void</span> ScribbleArea<span class="operator">::</span>drawLineTo(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span> <span class="operator">&amp;</span>endPoint)
  {
      <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="operator">&amp;</span>image);
      painter<span class="operator">.</span>setPen(<span class="type"><a href="../qtgui/qpen.html">QPen</a></span>(myPenColor<span class="operator">,</span> myPenWidth<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>SolidLine<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RoundCap<span class="operator">,</span>
                          <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RoundJoin));
      painter<span class="operator">.</span>drawLine(lastPoint<span class="operator">,</span> endPoint);
      modified <span class="operator">=</span> <span class="keyword">true</span>;

      <span class="type">int</span> rad <span class="operator">=</span> (myPenWidth <span class="operator">/</span> <span class="number">2</span>) <span class="operator">+</span> <span class="number">2</span>;
      update(<span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(lastPoint<span class="operator">,</span> endPoint)<span class="operator">.</span>normalized()
                                       <span class="operator">.</span>adjusted(<span class="operator">-</span>rad<span class="operator">,</span> <span class="operator">-</span>rad<span class="operator">,</span> <span class="operator">+</span>rad<span class="operator">,</span> <span class="operator">+</span>rad));
      lastPoint <span class="operator">=</span> endPoint;
  }

  <span class="type">void</span> ScribbleArea<span class="operator">::</span>resizeImage(<span class="type"><a href="../qtgui/qimage.html">QImage</a></span> <span class="operator">*</span>image<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> <span class="operator">&amp;</span>newSize)
  {
      <span class="keyword">if</span> (image<span class="operator">-</span><span class="operator">&gt;</span>size() <span class="operator">=</span><span class="operator">=</span> newSize)
          <span class="keyword">return</span>;

      <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> newImage(newSize<span class="operator">,</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span><span class="operator">::</span>Format_RGB32);
      newImage<span class="operator">.</span>fill(<a href="../qtgui/qcolor.html#qRgb">qRgb</a>(<span class="number">255</span><span class="operator">,</span> <span class="number">255</span><span class="operator">,</span> <span class="number">255</span>));
      <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="operator">&amp;</span>newImage);
      painter<span class="operator">.</span>drawImage(<span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span>)<span class="operator">,</span> <span class="operator">*</span>image);
      <span class="operator">*</span>image <span class="operator">=</span> newImage;
  }

  <span class="type">void</span> ScribbleArea<span class="operator">::</span>print()
  {
  <span class="preprocessor">#if QT_CONFIG(printdialog)</span>
      <span class="type">QPrinter</span> printer(<span class="type">QPrinter</span><span class="operator">::</span>HighResolution);

      <span class="type">QPrintDialog</span> printDialog(<span class="operator">&amp;</span>printer<span class="operator">,</span> <span class="keyword">this</span>);
      <span class="keyword">if</span> (printDialog<span class="operator">.</span>exec() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>Accepted) {
          <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="operator">&amp;</span>printer);
          <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> rect <span class="operator">=</span> painter<span class="operator">.</span>viewport();
          <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> size <span class="operator">=</span> image<span class="operator">.</span>size();
          size<span class="operator">.</span>scale(rect<span class="operator">.</span>size()<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>KeepAspectRatio);
          painter<span class="operator">.</span>setViewport(rect<span class="operator">.</span>x()<span class="operator">,</span> rect<span class="operator">.</span>y()<span class="operator">,</span> size<span class="operator">.</span>width()<span class="operator">,</span> size<span class="operator">.</span>height());
          painter<span class="operator">.</span>setWindow(image<span class="operator">.</span>rect());
          painter<span class="operator">.</span>drawImage(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> image);
      }
  <span class="preprocessor">#endif // QT_CONFIG(printdialog)</span>
  }

</pre>
</div>
<!-- @@@widgets/scribble/scribblearea.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
